package com.zfzs.post.modules.common.service.impl;

import com.zfzs.post.enums.TeamType;
import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.modules.common.domain.AgentTree;
import com.zfzs.post.modules.common.repository.AgentTreeRepository;
import com.zfzs.post.modules.common.service.AgentTreeService;
import com.zfzs.post.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author wq
 * @date 2019/10/16 11:40
 * @description 合伙人树形关系表
 */
@Service
public class AgentTreeServiceImpl implements AgentTreeService {

    @Autowired
    private AgentTreeRepository agentTreeRepository;


    @Override
    public AgentTree findOne(Long id) {
        Optional<AgentTree> p = agentTreeRepository.findById(id);
        return p.isPresent() ? p.get() : null;
    }

    @Override
    public void updateTree(Long agentId, Long pidId) {
        //增加代理商
        AgentTree t = new AgentTree();
        t.setAgentId(agentId);
        t.setParent(pidId);

        //增加上级代理商关联
        AgentTree tree = findByAgentId(pidId);
        if (tree == null) {
            throw new BadRequestException("未查询到上级关联信息");
        }
        String path = tree.getPath();
        path += StringUtils.isBlank(path) ? pidId : "," + pidId;
        t.setPath(path);
        agentTreeRepository.save(t);
    }

    @Override
    public AgentTree save(AgentTree agentTree) {
        return agentTreeRepository.save(agentTree);
    }

    @Override
    public AgentTree findByAgentId(Long agentId) {
        return agentTreeRepository.findByAgentId(agentId);
    }

    @Override
    public List<AgentTree> queryAll() {
        return agentTreeRepository.findAll();
    }

    @Override
    public List<Long> findAllTeamIds(Long agentId) {
        List<AgentTree> trees = findAllTeams(agentId);
        List<Long> collect = trees.stream().map(e -> e.getAgentId()).collect(Collectors.toList());
        if (collect == null || collect.size() == 0) {
            collect = new ArrayList<>();
            collect.add(0L);
        }
        return collect;
    }


    public List<AgentTree> findAllTeams(Long agentId) {
        return agentTreeRepository.findByPathRegex(agentId.toString());
    }

    public List<AgentTree> findByParent(Long agentId) {
        return agentTreeRepository.findByParent(agentId);
    }

    @Override
    public List<Long> findDirectlyIds(Long agentId) {
        List<AgentTree> trees = findByParent(agentId);
        List<Long> collect = trees.stream().map(e -> e.getAgentId()).collect(Collectors.toList());
        if (collect == null || collect.size() == 0) {
            collect = new ArrayList<>();
            collect.add(0L);
        }
        return collect;

    }

    @Override
    public List<Long> findAgentTeam(Long agentId) {
        //获取直营
        List<Long> directlyIds = findDirectlyIds(agentId);
        //获取所有合伙人
        List<Long> allTeamIds = findAllTeamIds(agentId);
        //剔除直营,返回团队
        List<Long> collect = allTeamIds.stream()
                .filter(e -> directlyIds.stream().noneMatch(e2 -> e2.equals(e)))
                .collect(Collectors.toList());

        if (collect == null || collect.size() == 0) {
            collect = new ArrayList<>();
            collect.add(0L);
        }
        return collect;
    }

    @Override
    public List<Long> findListByTeamType(String teamType, Long AgentId) {
        List<Long> teamList;
        if (StringUtils.isEmpty(teamType) || TeamType.TEAM.equalsIgnoreCase(teamType)) {
            teamList = findAllTeamIds(AgentId);
        } else if (TeamType.DIRECTLY.equalsIgnoreCase(teamType)) {
            teamList = findDirectlyIds(AgentId);
        } else if (TeamType.PARTNER.equalsIgnoreCase(teamType)) {
            teamList = findAgentTeam(AgentId);
        } else {
            throw new BadRequestException("类型不正确");
        }
        return teamList;
    }


}
